﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.Designer.UI.Editor
{
    /// <summary>
    /// DoubleEditor.xaml 的交互逻辑
    /// </summary>
    public partial class BrushEditor : PropertyValueEditor
    {
        private string BrushType=string.Empty;
        public BrushEditor()
        {
            InitializeComponent();
            txtValue.LostFocus += txtValue_LostFocus;
        }

        void txtValue_LostFocus(object sender, RoutedEventArgs e)
        {
            TextBox tb = sender as TextBox;
            if(string.IsNullOrWhiteSpace(tb.Text)) return;
            Brush brush = null;
            //if (BrushType.Equals("SolidColorBrush"))
            //{
                brush = new SolidColorBrush((Color)ColorConverter.ConvertFromString(txtValue.Text));
                ChangePropertyValue(brush);
            //}
            //else if (BrushType.Equals("ImageBrush"))
            //{
            //    BitmapImage bitMapImage = new BitmapImage(new Uri(txtValue.Text));
            //    brush = new ImageBrush(bitMapImage);
            //    ChangePropertyValue(brush);
            //}
            //else if (BrushType.Equals("LinearGradientBrush"))
            //{
            //    brush = new LinearGradientBrush();
            //    //string[] gsStrings = txtValue.Text.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            //    //if (gsStrings.Length > 0)
            //    //{ 
            //    //}
            //    //foreach (Metadata.ValueNode vN in valueNode.ValueNodes)
            //    //{
            //    //    if (!string.IsNullOrEmpty(vN.Attributes["Color"]))
            //    //    {
            //    //        GradientStop gs = new GradientStop((Color)ColorConverter.ConvertFromString(vN.Attributes["Color"]),
            //    //            System.Convert.ToDouble(vN.Attributes["Offset"]));
            //    //        lgb.GradientStops.Add(gs);
            //    //    }
            //    //}
            //    //ChangePropertyValue(brush);
            //}
        }
        protected override void OnPropertyValueChanged(object newValue)
        {
            if (newValue is SolidColorBrush)
            {
                BrushType = "SolidColorBrush";
                SolidColorBrush brush = newValue as SolidColorBrush;
                txtValue.Text = brush.Color.ToString();
            }
            else if (newValue is ImageBrush)
            {
                BrushType = "ImageBrush";
                ImageBrush brush = newValue as ImageBrush;
                BitmapImage bitmapImage = (BitmapImage)brush.ImageSource;
                txtValue.Text = bitmapImage.UriSource.AbsolutePath;

            }
            else if (newValue is LinearGradientBrush)
            {
                BrushType = "LinearGradientBrush";
                LinearGradientBrush brush = newValue as LinearGradientBrush;
                StringBuilder gsString = new StringBuilder();
                foreach (GradientStop gs in brush.GradientStops)
                {
                    gsString.AppendFormat("Color:{0}-Offset:{1}", gs.Color.ToString(), gs.Offset.ToString());
                    gsString.Append(",");
                }
                txtValue.Text = gsString.ToString().TrimEnd(',');
            }
        }
    }
}
